package com.example.back.impl;

import com.example.back.mapper.AdminMapper;
import com.example.back.mapper.RoomMapper;
import com.example.back.model.Admin;
import com.example.back.model.Employee;
import com.example.back.model.Record;
import com.example.back.service.AdminService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private RoomMapper roomMapper;

    @Override
    public List<Admin> findAllAdmin() {
        return adminMapper.findAllAdmin();
    }

    @Override
    public String adminLogin(String account, String password, HttpSession httpSession) {
        String rs;
        Admin admin = adminMapper.findByAccount(account);
        if (admin == null) {
            rs = "用户不存在";
        }
        else if (admin.getPassword().equals(password)) {
            httpSession.setAttribute("adminAccount", admin.getAccount());
            httpSession.setAttribute("adminPassword", admin.getPassword());
            rs = "成功";
        } else {
            rs = "失败";
        }
        return rs;
    }

    @Override
    public String userCount() {
        return adminMapper.userCount();
    }

    @Override
    public String orderCount() {
        return adminMapper.orderCount();
    }

    @Override
    public List<Employee> selectAllEmployee() {
        return adminMapper.selectAllEmployee();
    }

    @Override
    public List<Employee> selectByInformation(String information) {
        return adminMapper.selectByInformation(information);
    }

    @Override
    public Boolean insertEmployee(String name, int sex, Date birthday, String phone, String job) {
        return adminMapper.insertEmployee(name, sex, birthday, phone, job);
    }

    @Override
    public Boolean updateEmployeeJob(int employeeID, String job) {
        return adminMapper.updateEmployeeJob(employeeID, job);
    }

    @Override
    public Boolean deleteEmployee(int employeeID) {
        return adminMapper.deleteEmployee(employeeID);
    }

    @Override
    public List<Record> selectAllInto_room() {
        return adminMapper.selectAllInto_room();
    }

    @Override
    public List<Record> findInto_roomByRoom(String name) {
        return adminMapper.findInto_roomByRoom(name);
    }

    @Override
    public List<Record> findInto_roomByAccount(String account) {
        return adminMapper.findInto_roomByAccount(account);
    }

    @Override
    public List<Record> findInto_roomByFlag(int flag) {
        return adminMapper.findInto_roomByFlag(flag);
    }

    @Override
    public Boolean deleteRecordByID(int id) {
        return adminMapper.deleteRecordByID(id);
    }

    @Override
    public int findFlagByID(int id) {
        return adminMapper.findFlagByID(id);
    }

    @Override
    public Boolean updateRecordByID(int id) {
        int flag = adminMapper.findFlagByID(id);
        String name = adminMapper.findNameByID(id);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new java.util.Date());
        if (flag == 1) {
            return adminMapper.updateRecordPlayByID(id, time);
        }else if (flag == 2) {
            roomMapper.leaveRoom(name);
            if(roomMapper.findRoomCount(name) == 0)roomMapper.updateRoomRemark(name);
            return adminMapper.updateRecordLeaveByID(id, time);
        }
        else return false;
    }
}
